package com.umeox.watch.moto.pedometer.provider;

import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.dbflow5.config.FlowManager;
import com.dbflow5.provider.BaseContentProvider;
import com.umeox.moto.common.log.Logger;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.pedometer.base.BaseDatabase;
import com.umeox.watch.moto.pedometer.base.PedometerDBHelper;
import com.umeox.watch.moto.pedometer.base.StepInfo;
import com.umeox.watch.moto.pedometer.util.CacheImpl;

public class StepsProvider extends BaseContentProvider {

    private static final int STEPINFO_CONTENT_URI = 0;

    private final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

    @Override
    public final boolean onCreate() {
        MATCHER.addURI(DataProvider.StepCountProvider.AUTHORITY, "Steps", STEPINFO_CONTENT_URI);
        return true;
    }

    @Override
    public final String getDatabaseName() {
        return FlowManager.getDatabaseName(BaseDatabase.class);
    }

    @Override
    public final String getType(Uri uri) {
        String type = null;
        switch (MATCHER.match(uri)) {
            case STEPINFO_CONTENT_URI: {
                type = "vnd.android.cursor.dir/steps";
                break;
            }
            default: {
                throw new IllegalArgumentException("Unknown URI" + uri);
            }
        }
        return type;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        try {
            return FlowManager.getDatabase(BaseDatabase.class).getWritableDatabase().query(StepInfo.TABLE_NAME, projection, selection, selectionArgs, sortOrder, null, sortOrder);

        } catch (Exception e) {
            Log.e("stepsProvider", "query: ", e);
        }

        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    protected final int bulkInsert(Uri uri, ContentValues values) {
        return 0;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        switch (MATCHER.match(uri)) {
            case STEPINFO_CONTENT_URI: {
                PedometerDBHelper.getInstance().removeALlEntries();
                CacheImpl.getInstance().clearAll();
                Logger.w("清空计步数据");
                return 0;
            }
            default: {
                throw new IllegalArgumentException("Unknown URI" + uri);
            }
        }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }
}
